<?php
namespace Admin\Controller;
class RoleController extends BaseController 
{
    public function add()
    {
    	if(IS_POST)
    	{
    		//var_dump(I('post.'));die();
    		$model = D('Role');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

    	// 取出所有的权限
		$priModel = D('Privilege');
		$priData = $priModel->getTree();
		//echo "<pre>";
		//var_dump($priData);die();
		$roleModel = D('Role');
    	$data = $roleModel->search1();
    	//echo '<pre>';
    	//print_r($data);
		// 设置页面中的信息
		$this->assign(array(
			'priData' => $priData,
		));
		$this->display();
    }
    public function lst(){
    	$roleModel = D('role');
    	$data = $roleModel->search1();

    	$count = $roleModel->count();
        // var_dump($count);die;
    	$this->assign(array(
    		'data'=>$data,
            'count'=>$count,

    	));
    	//echo '<pre>';
    	//var_dump($data);die();
    	$this->display();
    }
    public function edit(){
    	
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		//echo '<pre>';
    		//var_dump(I('post.'));die();
    		$model = D('Role');
    		//$data = $model->create(I('post.'));
    		//var_dump($data);die();
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FLASE)
    			{
    				$this->success('修改成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	 
    	//echo $id;
    	$priModel = D('Privilege');
    	$priData = $priModel->getTree();
    	//查找要求修改的ID
    	$rolModel = D('role');
    	$info = $rolModel->find($id);
    	//var_dump($info);die();
    	//取出这个角色拥有的权限
    	$rol_pri_Model = D('role_pri');
    	$priIdData = $rol_pri_Model->field('GROUP_CONCAT(pri_id) pri_id')->where(array(
    		'role_id'=>array('eq',$id),
    	))->find();
    	//var_dump($priIdData);die();
    	$this->assign(array(
    		'priData'=>$priData,
    		'info'   =>$info,
    		'priIdData'=>$priIdData['pri_id'],
    	));
    	$this->display();
    }
    //删除
    public function delete(){
    	$id = I('get.id');
    	$roleModel = D('role');
    	if($roleModel->delete($id) !== FALSE){
    		$this->success('删除成功',U('lst'));
    	}else{
    		$error = $roleModel->getError();
    		$this->error($error);
    	}
    }

}